package com.baoyouqun.controller.back;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baoyouqun.base.PageResponse;
import com.baoyouqun.domain.CreateCmd.CityPartnerApplicationCreateCmd;
import com.baoyouqun.domain.Query.CityPartnerApplicationQuery;
import com.baoyouqun.domain.UpdateCmd.CityPartnerApplicationUpdateCmd;
import com.baoyouqun.domain.VO.CityPartnerApplicationVO;
import com.baoyouqun.service.CityPartnerApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/back/cityPartnerApplication")
@Api(tags = "城市合伙人申请表管理接口")
public class CityPartnerApplicationController {

    @Resource
    private CityPartnerApplicationService cityPartnerApplicationService;

    @PostMapping("create")
    @ApiOperation("新增城市合伙人申请表")
    public SingleResponse<CityPartnerApplicationVO> create(CityPartnerApplicationCreateCmd cmd) {
        return cityPartnerApplicationService.create(cmd);
    }

    @PutMapping("update")
    @ApiOperation("修改城市合伙人申请表")
    public Response update(CityPartnerApplicationUpdateCmd cmd) {
        return cityPartnerApplicationService.update(cmd);
    }

    @DeleteMapping("delete")
    @ApiOperation("根据ID删除城市合伙人申请表")
    public Response delete(String id) {
        StpUtil.checkRole("R010000");
        cityPartnerApplicationService.removeById(id);
        return Response.buildSuccess();
    }

    @GetMapping("getById")
    @ApiOperation("根据ID查询城市合伙人申请表")
    public SingleResponse<CityPartnerApplicationVO> selectById(String id) {
        return SingleResponse.of(cityPartnerApplicationService.selectById(id));
    }

    @GetMapping("page")
    @ApiOperation(value = "条件筛选分页查询", notes = "支持申请人、手机号、处理状态筛选，默认未处理（1），每页10条")
    public PageResponse<CityPartnerApplicationVO> page(
            @ApiParam(value = "筛选条件（申请人/手机号/状态）", required = false)
            @ModelAttribute CityPartnerApplicationQuery query) {
        IPage<CityPartnerApplicationVO> page = cityPartnerApplicationService.pageVo(query);
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @PostMapping("export")
    @ApiOperation(value = "导出申请记录Excel", notes = "按当前筛选条件导出全量数据（申请人/手机号/行业等）")
    public void exportApplication(
            @ApiParam(value = "筛选条件（与分页一致）", required = false)
            @RequestBody(required = false) CityPartnerApplicationQuery query,
            @ApiParam(value = "下载响应流", hidden = true)
            HttpServletResponse response) {
        cityPartnerApplicationService.exportApplicationExcel(query, response);
    }

    @PutMapping("updateStatus")
    @ApiOperation(value = "更新处理状态", notes = "设为未处理（1）或已处理（2），对应页面“设为未处理/已处理”按钮")
    public Response updateApplicationStatus(
            @ApiParam(value = "申请记录ID", required = true, example = "123456")
            @RequestParam String id,
            @ApiParam(value = "目标状态（1-未处理，2-已处理）", required = true, example = "2")
            @RequestParam Byte status) {
        return cityPartnerApplicationService.updateStatus(id, status);
    }
}